import requests
from requests import RequestException


class RequestsUtils:
    def __init__(self):
        self.session = requests.Session()

    def send_requests(self, method, url, params=None, data=None, json=None, **kwargs):
        # 确保HTTP方法不区分大小写
        method = method.lower()

        try:
            if method == 'get':
                resp = self.session.request(method, url=url, params=params, **kwargs)
            else:
                resp = self.session.request(method, url=url, params=params, data=data, json=json, **kwargs)

            # 确保响应成功，否则抛出异常
            resp.raise_for_status()
        except RequestException as e:
            # 处理请求异常，例如记录日志、重试等
            print(f"An error occurred: {e}")
            raise

        return resp
